
module game {

    export class LoginMediator extends BaseMediator {
        public static NAME: string = "LoginMediator";

        public constructor(viewComponent: any = null) {
            super(LoginMediator.NAME, viewComponent);
        }
        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.loginPanel.okBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.sendMsgButtonClick, this);
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {


        }

        public onRegister() {
            super.onRegister();

            this.addSocketHandler();
        }
        /**
         * 处理客户端-服务器的收发请求
         */
        public addSocketHandler(): void {
            GameMain.me.evNet.addEventListener(ProtoID.LoginLoginServer_Response, this.loginRsp, this);
        }

        private loginRsp(ev: NetEvent) {
            let playerData: GameVO = (this.facade.retrieveProxy(GameProxy.NAME) as GameProxy).vo_;
            playerData.gameName = ev.data.gameName;
            this.closePanel(0);
        }
        public listNotificationInterests(): Array<any> {
            return [
                SceneNotify.OPEN_LOGIN,
            ];
        }
        private loginPanel: LoginPanel;
        /**
         * 处理其他地方传过来的请求
         */
        public handleNotification(notification: puremvc.INotification): void {
            var data: any = notification.getBody();
            switch (notification.getName()) {
                case SceneNotify.OPEN_LOGIN: {
                    if (!this.loginPanel) {
                        this.loginPanel = new LoginPanel();
                    }
                    //显示
                    this.showUI(this.loginPanel, false, 0, 0, 0);
                    break;
                }
            }
        }



        private sendMsgButtonClick(event: egret.TouchEvent): void {
            this.closePanel(0);
            this.sendNotification(SceneNotify.OPEN_HOME);
            this.sendNotification(MainNotify.OPEN_MAIN);
            return;
            var msg = GameMain.me.proto.msgnew("login.LoginLoginServer_Request");//协议名都能从proto文件中找到
            msg.mahjong = 1;
            SocketManager.me.startSend(msg);
        }

        private closeButtonClick(event: egret.TouchEvent): void {
            this.closePanel(0);
        }
    }
}